package com.bjpowernode.boss.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.boss.common.enums.AjaxCode;
import com.bjpowernode.boss.convert.UserInfoConvert;
import com.bjpowernode.boss.db.domain.UserInfo;
import com.bjpowernode.boss.web.service.UserInfoService;
import com.bjpowernode.boss.web.models.form.StatusForm;
import com.bjpowernode.boss.web.models.result.AjaxResult;
import com.bjpowernode.boss.web.models.vo.UserInfoVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息控制器
 *
 * @author shenlang
 * @version 1.0.0
 * @date 2024/5/30 10:06
 */
@Controller
@ResponseBody
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserInfoConvert userInfoConvert;
    /**
     * 获取用户分页1
     *
     * @param page 当前页
     * @param size 页面呈现数量
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-01 19:10:52 19:10
     */
    @RequestMapping("/user/getPageList")
    public AjaxResult getPageList(Integer page, Integer size){
        page = (page==null||page<1?1:page);
        IPage<UserInfo> ipage = userInfoService.getPageInfo(page,size);
        long total = ipage.getTotal();
        long current = ipage.getCurrent();
        List<UserInfo> userInfos = ipage.getRecords();
        List<UserInfoVo> userInfoVos = userInfoConvert.userInfoVos(userInfos);
        Map<String,Object> data = new HashMap<>();
        data.put("currentPage",current);
        data.put("totalPage",total);
        data.put("list",userInfoVos);
        return new AjaxResult(AjaxCode.OBTAINED_USERINFO_SUCCESS, data);
    }
    /**
     * 获取自定义用户信息
     *
     * @param page 当前页
     * @param pageSize 页面最大表数据的数量
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-01 19:30:29 19:30
     */
    @RequestMapping("/user/getPage")
    public AjaxResult getPage(Integer page,Integer pageSize){
        Map<String,Object> data = userInfoService.getPageMap(page,pageSize);
        return new AjaxResult(AjaxCode.OBTAINED_USERINFO_SUCCESS, data);
    }
    /**
     * 获取所有用户信息
     *
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-01 19:25:47 19:25
     */
    @RequestMapping("/user/getUserTable")
    public AjaxResult getUserTable(){
        List<UserInfo> userInfos = userInfoService.getUserList();
        List<UserInfoVo> userInfoVos = userInfoConvert.userInfoVos(userInfos);
        return new AjaxResult(AjaxCode.USERINFO_GET_SUCCESS,userInfoVos);
    }
    /**
     * 改变用户状态
     *
     * @param statusForm  status信息
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-01 19:19:45 19:19
     */
    @RequestMapping("/user/changStatus")
    public AjaxResult changStatus(@RequestBody StatusForm statusForm){
        Boolean bool = userInfoService.changStatus(statusForm.getId(),statusForm.getStatus());
        return new AjaxResult(AjaxCode.CHANGE_STATUS_SUCCESS,bool);
    }
    /**
     * 单行删除
     *
     * @param id 用户id
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-01 19:34:54 19:34
     */
    @RequestMapping("/user/singleDeleteUser")
    public AjaxResult singleDeleteUser(@RequestParam String id){
        boolean bool = userInfoService.singleDelete(id);
        return new AjaxResult(AjaxCode.DELETE__SUCCESS,bool);
    }
    /**
     * 批量删除
     *
     * @param ids id集合
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-01 21:21:25 21:21
     */
    @RequestMapping("/user/batchDeleteUser")
    public AjaxResult batchDeleteUser(@RequestParam List<String> ids){
        boolean bool = userInfoService.batchDelete(ids);
        return new AjaxResult(AjaxCode.DELETE__SUCCESS,bool);
    }
    /**
     * 增加用户
     *
     * @param userInfo 用户信息表
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-02 20:03:33 20:03
     */
    @RequestMapping("/user/addUser")
    public AjaxResult addUser(@RequestBody UserInfo userInfo){
        boolean bool = userInfoService.addUserInfo(userInfo);
        return new AjaxResult(bool?AjaxCode.ADD__SUCCESS:AjaxCode.ADD_FAILURE,bool);
    }
    /**
     * 编辑用户
     *
     * @param userInfo 接收用户信息
     * @return com.bjpowernode.boss.web.models.result.AjaxResult
     * @date 2024-06-02 21:24:03 21:24
     */
    @RequestMapping("/user/editUser")
    public AjaxResult editUser(@RequestBody UserInfo userInfo){
        boolean bool = userInfoService.editUserInfo(userInfo);
        return new AjaxResult(AjaxCode.EDIT__SUCCESS,bool);
    }
}
